package com.yzkj.siot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzkj.siot.context.CurUserCtx;
import com.yzkj.siot.domain.TenantDeviceData;
import com.yzkj.siot.dto.DataExportDTO;
import com.yzkj.siot.mapper.TenantDeviceDataMapper;
import com.yzkj.siot.result.PageParams;
import com.yzkj.siot.service.TenantDeviceDataService;
import com.yzkj.siot.vo.DeviceDataItemVO;
import com.yzkj.siot.vo.DeviceDataSummaryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备
 * 服务实现类
 * </p>
 *
 * @author skye
 * @since 2021-11-07
 */
@Service
@Slf4j
public class TenantDeviceDataServiceImpl extends ServiceImpl<TenantDeviceDataMapper, TenantDeviceData> implements TenantDeviceDataService {

    @Override
    public List<DeviceDataSummaryVO> querySummary(Integer timeGranularity, Integer productDataId, String startTime, String endTime) {

        Integer tenantId = CurUserCtx.get().getTenantId();

        // 小时
        if (timeGranularity == 2) {
            return baseMapper.querySummaryForHour(productDataId, startTime, endTime, tenantId);
        }

        startTime += " 00:00:00";
        endTime += " 23:59:59";
        List<DeviceDataSummaryVO> deviceDataList = baseMapper.querySummary(timeGranularity, productDataId, startTime, endTime, tenantId);
        LinkedList<DeviceDataSummaryVO> res = new LinkedList<>();
        List<String> timeList = new ArrayList<>();
        for (DeviceDataSummaryVO item : deviceDataList) {
            timeList.addAll(item.getItems().stream().map(DeviceDataItemVO::getDataCreateTime).collect(Collectors.toList()));
        }
        timeList = timeList.stream().distinct().collect(Collectors.toList());

        for (DeviceDataSummaryVO groupItem : deviceDataList) {
            DeviceDataSummaryVO newSummaryVO = new DeviceDataSummaryVO();
            newSummaryVO.setDeviceId(groupItem.getDeviceId());
            newSummaryVO.setDeviceName(groupItem.getDeviceName());
            newSummaryVO.setItems(new LinkedList<>());
            newSummaryVO.getItems().addAll(groupItem.getItems());
            for (int i = 0; i < groupItem.getItems().size(); i++) {
                for (int j = 0; j < timeList.size(); j++) {
                    String timeItem = timeList.get(j);
                    List<String> deviceTimes = groupItem.getItems().stream().map(DeviceDataItemVO::getDataCreateTime).collect(Collectors.toList());
                    if (!deviceTimes.contains(timeItem)) {
                        DeviceDataItemVO itemVO = new DeviceDataItemVO();
                        itemVO.setDataContentAvg(null);
                        itemVO.setDataCreateTime(timeItem);
                        LinkedList<DeviceDataItemVO> temp = new LinkedList<>(newSummaryVO.getItems());
                        temp.add(j, itemVO);
                        newSummaryVO.setItems(temp);
                    }
                }
            }
            res.add(newSummaryVO);
        }
        return res;
    }

    @Override
    public Integer curMonthCount() {

        Integer tenantId = CurUserCtx.get().getTenantId();
        Integer curMonthCount = baseMapper.curMonthCount(tenantId);
        return curMonthCount == null ? 0 : curMonthCount;
    }

    @Override
    public List<DataExportDTO> pageExportData(int tenantId, int productId, int productDataId, int deviceId, PageParams<DataExportDTO> pageParam) {

        return this.baseMapper.pageExportData(tenantId, productId, productDataId, deviceId, pageParam.getPage(), pageParam.getLimit());
    }

    @Override
    public Integer countExportData(int tenantId, int productId, int productDataId, int deviceId) {

        return this.baseMapper.countExportData(tenantId, productId, productDataId, deviceId);
    }
}
